package com.demo.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;

public class AppGlobal {
	private static Properties properties = null;
	private static String file = "setting.properties";

	private static void load() {
		if (properties == null) {
			properties = new Properties();
			FileInputStream fis;
			try {
				fis = new FileInputStream(file);
				properties.load(fis);
			} catch (IOException e) {
				e.printStackTrace();
			}

			properties.list(System.out);
			System.out.println("\nThe foo property: " + properties.getProperty("foo"));
		}
	}

	public static String get(String key) {
		AppGlobal.load();
		return properties.getProperty(key);
	}

	public static void set(String key, String value) {
		AppGlobal.load();
		properties.setProperty(key, value);
	}

	public static boolean save(String key, String value) {
		try {
			PrintStream fW = new PrintStream(new File(file));
			properties.list(fW);
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		}
	}
}
